package com.cbs.module.friend.friendmodule;

import android.content.Intent;
import com.cbs.module.friend.ModuleHandler;
import com.cbs.module.friend.entity.FriendRequest;
import com.cbs.application.activity.CBSActivity;
import com.cbs.application.activity.CBSSimpleActivityResultHandler;
import com.cbs.applicationutils.Global;
import com.cbs.module.user.UserModule;
import com.cbs.module.user.ui.UserModuleUI;
import com.cbs.module.user.ui.network.CBSUserUIRequest;
import com.cbs.network.Request;
import com.cbs.network.Response;
import com.cbs.network.ResponseHandler;
import com.cbs.network.decoder.GsonDecoder;
import com.cbs.runtimecache.RuntimeCache;
import com.squareup.okhttp.FormEncodingBuilder;

import java.net.HttpURLConnection;

/**
 * Created by broche on 1/16/16.
 */
public class FriendRequestAdd {
    private final String FriendRequestListUrlTpl = "%s/wechat/api/v1/users/%s/friendrequests";
    private int friendUserId;
    private CBSActivity context;
    private ModuleHandler<FriendRequest> moduleHandler;
    private RuntimeCache runtimeCache;

    public FriendRequestAdd(CBSActivity context, int friendUserId, ModuleHandler<FriendRequest> moduleHandler, RuntimeCache runtimeCache) {
        this.context = context;
        this.friendUserId = friendUserId;
        this.moduleHandler = moduleHandler;
        this.runtimeCache = runtimeCache;
    }

    public void start() {
        if (UserModule.getUser() == null || !UserModule.getUser().isRegistered()) {
            UserModuleUI.login(context, new CBSSimpleActivityResultHandler() {
                @Override
                public void onSuccess(int requestCode, Intent data) {
                    friendRequestAddRequest();
                }

                @Override
                public void onCancel(int requestCode, Intent data) {
                    moduleHandler.onFailure(HttpURLConnection.HTTP_FORBIDDEN, "forbidden");
                }
            });
        } else {
            friendRequestAddRequest();
        }
    }


    private void friendRequestAddRequest() {
        Request request = new CBSUserUIRequest(context, String.format(FriendRequestListUrlTpl, Global.getServerHost(), friendUserId + ""))
                .setDecoder(new GsonDecoder(FriendRequest.class))
                .post(new FormEncodingBuilder().build())
                .setResponseHandler(new ResponseHandler() {
                    @Override
                    public void onResponse(Response response) {
                        if (response.isSuccessful()) {
//                            FriendRequest friendRequest = ((FriendRequest) response.getResult());
//                            List<FriendRequest> requests = (List<FriendRequest>) runtimeCache.getValue(FriendRequestListCache.class);
//                            boolean existed = false;
//                            for (int i = 0; i < requests.size(); i++) {
//                                FriendRequest trequest = requests.get(i);
//                                if (trequest.getId() == friendRequest.getId()) {
//                                    existed = true;
//                                    break;
//                                }
//                            }
//                            if (!existed) {
//                                requests.add(friendRequest);
//                                runtimeCache.setValue(FriendRequestListCache.class, requests);
//                            }
                            moduleHandler.onSuccess(null);
                        } else {
                            moduleHandler.onFailure(response.getCode(), response.getRequest().toString());
                        }
                    }

                    @Override
                    public void onException(Request request, Exception e) {
                        moduleHandler.onException(e);
                    }
                });
        Global.getHttpClient().clone().send(request);
    }
}
